/***********************************************************************************************************************
/*
/*  low_priority_thread.cpp
/*
/**********************************************************************************************************************/

#include "stdafx.h"


//======================================================================================================================
//  CODE
//======================================================================================================================

//----------------------------------------------------------------------------------------------------------------------
//  UPDATE GAMMA
//----------------------------------------------------------------------------------------------------------------------
void update_gamma ()
{
	if ( ! g_Settings.bOn )
		return;

	static HWND hwndPrev = 0;

	HWND hwnd = GetForegroundWindow();

	if ( hwndPrev == hwnd )
		return;

	hwndPrev = hwnd;
			
	wchar_t awClassName [ 1000 ];

	GetClassName ( hwnd, awClassName, sizeof(awClassName)/sizeof(wchar_t) );

	if ( g_hwnd == hwnd || g_hwndMini == hwnd || 0 == wcscmp ( L"DAoCMWC", awClassName ) )
		set_gamma_ramp ( ((double) g_Settings.uGamma ) / 100, g_Settings.uBrightness, g_Settings.uContrast );

	else
		set_default_gamma ();
}


//----------------------------------------------------------------------------------------------------------------------
//  LOW PRIORITY THREAD
//----------------------------------------------------------------------------------------------------------------------
unsigned _stdcall low_priority_thread ( void * )
{
	for (;;)
	{
		update_gamma ();

		Sleep (500);
	}
}


//----------------------------------------------------------------------------------------------------------------------
//  START LOW PRIORITY THREAD
//----------------------------------------------------------------------------------------------------------------------
void start_low_priority_thread ()
{
	HANDLE hThread = reinterpret_cast<HANDLE> ( _beginthreadex ( NULL, 0, low_priority_thread, 0, 0, 0 ) );
	SetThreadPriority ( hThread, THREAD_PRIORITY_BELOW_NORMAL );
}


